from django.db import models

SWAP_MAKERS = [
    ('玖行', '玖行'),
    ('融青', '融青'),
    ('智锂', '智锂'),
    ('双杰', '双杰'),
    ('三一', '三一'),
]
SOFTWARE_VERSIONS = [
    ('2', '玖行站控'),
    ('7', '自研站控'),
]
GROUP_IDS = [
    ('cidLSNmQcBGApGfJ2fs7s/ZwA==', '事业一部'),
    ('cid3tEqV2PxMArhN43tS4K5+w==', '事业二部'),
    ('cidROVaJHQAgYcipeqZGaXUPQ==', '事业三部'),
    ('cid1a02dNB/KB3epiHv8xR9GQ==', '事业四部'),
    ('cidq+M7ADQGkt4r1g99QbJ/og==', '事业五部'),
    ('cidRU9GZuy+Kun62rYjtTxV6w==', '事业六部'),
]
BUSINESS_UNITS = [
    ('事业一部', '事业一部'),
    ('事业二部', '事业二部'),
    ('事业三部', '事业三部'),
    ('事业四部', '事业四部'),
    ('事业五部', '事业五部'),
    ('事业六部', '事业六部'),
]
DRIVE_TYPES = [
    ('inverter', '变频器驱动'),
    ('servo', '伺服驱动'),
    ('hydraulic', '液压驱动'),
]
ROBOT_DIRECTIONS = [
    ('vertical', '垂直'),
    ('parallel', '平行'),
]
STATISTICAL_METHODS = [
    ('min', '最小值'),
    ('max', '最大值'),
    ('mean', '平均值'),
    ('median', '中位数'),
    ('sum', '总和'),
    ('std', '标准差')
]
PHYSICAL_QUANTITIES = [
    ('current1', '大车变频器电流'),
    ('current2', '小车变频器电流'),
    ('current3', '起升变频器电流'),
    ('frequency1', '大车变频器频率'),
    ('frequency2', '小车变频器频率'),
    ('frequency3', '起升变频器频率'),
]


class PhysicalQuantity(models.Model):
    quantity_name = models.CharField(max_length=20, primary_key=True,
                                     db_column='quantityName', verbose_name="量纲名称")
    quantity_description = models.CharField(max_length=45, db_column='quantityDesc', verbose_name='量纲描述')

    class Meta:
        db_table = "insight_PHYSICAL_QUANTITY"
        verbose_name = "物理量"
        managed = False


class StatisticalMethod(models.Model):
    method_name = models.CharField(max_length=20, primary_key=True,
                                   db_column='methodName', verbose_name='方法名称')
    method_description = models.CharField(max_length=45, db_column='methodDesc', verbose_name="方法描述")

    class Meta:
        db_table = "insight_STATISTICAL_METHOD"
        verbose_name = "统计方法"


# Create your models here.
class StationType(models.Model):
    swap_maker = models.CharField(max_length=10, choices=SWAP_MAKERS, db_column="swap_maker", verbose_name="换电站厂商")
    station_model = models.CharField(primary_key=True, db_column="station_model", max_length=20, verbose_name="换电站型号")

    class Meta:
        db_table = "insight_SWAP_STATION_TYPE"
        verbose_name = "换电站类型"


class RobotType(models.Model):
    swap_maker = models.CharField(max_length=10, db_column="swap_maker", choices=SWAP_MAKERS, verbose_name="换电站厂商")
    robot_type = models.CharField(primary_key=True, db_column="robot_type", max_length=20, verbose_name="换电机器人型号")

    class Meta:
        db_table = "insight_SWAP_ROBOT_TYPE"
        verbose_name = "换电机器人型号"


class Station(models.Model):
    station_id = models.CharField(max_length=10, primary_key=True, verbose_name="换电站编号", db_column='stationNo')
    station_name = models.CharField(max_length=40, db_column='stationName', verbose_name="换电站名称")
    business_unit = models.CharField(choices=BUSINESS_UNITS, max_length=10, db_column='businessUnit')
    group_id = models.CharField(max_length=40, choices=GROUP_IDS, db_column='groupID')
    station_maker = models.CharField(choices=SWAP_MAKERS, default='玖行', max_length=10, db_column='stationMaker')
    station_type = models.CharField(max_length=20, db_column="stationType", verbose_name="换电站类型")
    software_version = models.CharField(choices=SOFTWARE_VERSIONS, default='7', max_length=10,
                                        db_column='softwareVersion')
    robot_type = models.CharField(max_length=20, db_column="robotType", verbose_name="换电机器人型号")
    drive_type = models.CharField(max_length=10, choices=DRIVE_TYPES, db_column='driveType')
    robot_direction = models.CharField(max_length=10, choices=ROBOT_DIRECTIONS, db_column='robotDirection')
    motor_count = models.IntegerField(db_column="motorCount", verbose_name="电机数量")

    class Meta:
        db_table = "insight_swap_station"
        verbose_name = "换电站列表"
        managed = False


class SwapService(models.Model):
    SWAP_MODES = [
        (1, '全自动换电'),
        (2, '半自动换电'),
        (3, '人工干预'),
        (4, '二维码换电')
    ]
    SWAP_TYPES = [
        ('换电', '换电'),
        ('换仓', '换仓'),
    ]
    station_id = models.CharField(max_length=10, verbose_name="换电站", db_column="stationNo")
    order_sn = models.CharField(max_length=40, db_column="orderSn", primary_key=True, verbose_name="换电订单编号")
    rfid_code = models.CharField(max_length=50, db_column="rfidCode", verbose_name="车辆RFID", null=True)
    vin = models.CharField(max_length=20, db_column="vin", null=True, verbose_name="车辆vin码")
    plate_number = models.CharField(max_length=20, db_column="plateNo", verbose_name="车牌号码", null=True)
    start_time = models.DateTimeField(db_column="startTime", verbose_name="开始换电时间")
    stop_time = models.DateTimeField(db_column="stopTime", verbose_name="完成换电时间")
    summary_time = models.IntegerField(db_column="sumTimeSecond", verbose_name="换电时长")
    swap_mode = models.IntegerField(db_column="swapMode", choices=SWAP_MODES, verbose_name="换电模式")
    swap_type = models.CharField(max_length=10, db_column="swapType", choices=SWAP_TYPES, verbose_name="换电/换仓")

    class Meta:
        db_table = "insight_swap_service"
        verbose_name = "换电服务记录"
        managed = False


class SwapServiceFeature(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    station_id = models.CharField(max_length=10, verbose_name="换电站", db_column="stationNo")
    order_sn = models.CharField(max_length=100, verbose_name="所属换电服务记录", db_column="orderSn")
    feature_date = models.DateField(db_column="feature_date", verbose_name="特征日期")
    physical_quantity = models.CharField(max_length=20, db_column="physicalQuantity", verbose_name="物理量")
    statistical_method = models.CharField(max_length=20, choices=STATISTICAL_METHODS, db_column="statisticalMethod",
                                          verbose_name="统计方法")
    feature_value = models.FloatField(verbose_name="特征值", db_column="featureValue")

    class Meta:
        db_table = "insight_swap_service_feature"
        verbose_name = "换电过程特征"
        managed = False


class DailyFeature(models.Model):
    id = models.AutoField(primary_key=True, db_column='id')
    station_id = models.CharField(max_length=10, verbose_name="换电站", db_column="stationNo")
    feature_date = models.DateField(db_column="feature_date", verbose_name="特征日期")
    physical_quantity = models.CharField(max_length=20, db_column="physicalQuantity", verbose_name="物理量")
    statistical_method = models.CharField(max_length=20, choices=STATISTICAL_METHODS, db_column="statisticalMethod",
                                          verbose_name="统计方法")
    feature_value = models.FloatField(verbose_name="特征值", db_column="featureValue")

    class Meta:
        db_table = "insight_daily_feature"
        verbose_name = "日均特征"
        managed = False


class FaultPrediction(models.Model):
    prediction_id = models.AutoField(primary_key=True, db_column="prediction_id")
    predict_method = models.CharField(max_length=32, verbose_name="预测方式", db_column="predict_method")
    station_id = models.CharField(max_length=10, verbose_name="换电站编号", db_column="stationNo")
    physical_quantity = models.CharField(max_length=10, verbose_name="特征物理量", db_column="physicalQuantity")
    statistical_method = models.CharField(max_length=10, verbose_name="统计方法", db_column="statisticalMethod")
    predict_date = models.DateField(verbose_name="预测日期", db_column="predict_date")
    feature_date = models.DateField(verbose_name="特征日期", db_column="feature_date")
    feature_days = models.IntegerField(verbose_name="特征日期天数", db_column="feature_days")
    sigma = models.FloatField(verbose_name="sigma", db_column="sigma")
    feature_value = models.FloatField(verbose_name="特征值", db_column="feature_value")
    upper_limit = models.FloatField(verbose_name="特征上限值", db_column="upper_limit")
    down_limit = models.FloatField(verbose_name="特征下限值", db_column="down_limit")

    class Meta:
        db_table = "insight_fault_prediction"
        verbose_name = "换电机器人故障预测"
        managed = False
